//
//  GJSearchTableView.m
//  xmgjAppFront
//
//  Created by Zeki on 2020/7/29.
//  Copyright © 2020 guojutech. All rights reserved.
//

#import "GJSearchTableView.h"



@interface GJSearchTableViewCell ()
@property (nonatomic, strong)NSDictionary *poiInfo;
@property (nonatomic, strong)UILabel *poiNameLabel;
@property (nonatomic, strong)UILabel *poiAddressLabel;

@end

@implementation GJSearchTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
  NSString *cellID = [NSString stringWithFormat:@"%@ID", NSStringFromClass([self class])];
  GJSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
  if (!cell) {
    cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
  }
  return cell;
}



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    [self.contentView addSubview:self.poiAddressLabel];
    [self.contentView addSubview:self.poiNameLabel];
    
    [self.poiNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.mas_equalTo(16);
      make.height.mas_equalTo(23);
      make.top.mas_equalTo(12);
      make.right.mas_equalTo(-16);
    }];
    [self.poiAddressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.right.equalTo(self.poiNameLabel);
      make.bottom.mas_equalTo(-12);
      make.top.equalTo(self.poiNameLabel.mas_bottom).offset(4);
    }];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
  }
  return self;
}
- (void)drawRect:(CGRect)rect{
  [super drawRect:rect];
  CGContextRef currentContext = UIGraphicsGetCurrentContext();
  //设置虚线颜色
  CGContextSetStrokeColorWithColor(currentContext, HexColor(#E6E6E5).CGColor);
  //设置虚线宽度
  CGContextSetLineWidth(currentContext, 0.5);
  //设置虚线绘制起点
  CGContextMoveToPoint(currentContext, 16,rect.size.height - 0.5);
  //设置虚线绘制终点
  CGContextAddLineToPoint(currentContext, rect.size.width - 16, rect.size.height - 0.5);
  //设置虚线排列的宽度间隔:下面的arr中的数字表示先绘制3个点再绘制1个点
  CGFloat arr[] = {3,3};
  //下面最后一个参数“2”代表排列的个数。
  CGContextSetLineDash(currentContext, 0, arr, 2);
  CGContextDrawPath(currentContext, kCGPathStroke);
}



#pragma mark -- getter
- (UILabel *)poiNameLabel{
  if (!_poiNameLabel) {
    _poiNameLabel = [UILabel new];
    _poiNameLabel.textAlignment = NSTextAlignmentLeft;
    _poiNameLabel.textColor = HexColor(#343332);
    _poiNameLabel.font = kMediumSystemFontOfSize(16);
    _poiNameLabel.backgroundColor = [UIColor clearColor];
    
  }
  return _poiNameLabel;
}
- (UILabel *)poiAddressLabel{
  if (!_poiAddressLabel) {
    _poiAddressLabel = [UILabel new];
    _poiAddressLabel.font = kSystemFontOfSize(12);
    _poiAddressLabel.textColor = HexColor(#999896);
    _poiAddressLabel.textAlignment = NSTextAlignmentLeft;
    _poiAddressLabel.backgroundColor = [UIColor clearColor];
  }
  return _poiAddressLabel;
}

#pragma mark -- setter
- (void)setPoiInfo:(NSDictionary *)poiInfo{
  _poiInfo = poiInfo;
  self.poiNameLabel.text = poiInfo[@"name"];
  self.poiAddressLabel.text = poiInfo[@"address"];

}
@end




@interface GJSearchTableView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation GJSearchTableView


- (instancetype)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.edges.equalTo(self);
    }];
  }
  return self;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  [self endEditing:YES];
}

#pragma mark -- UITableViewDelegate && UITableViewSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  GJSearchTableViewCell *cell = [GJSearchTableViewCell cellWithTableView:tableView];
  cell.poiInfo = self.dataArray[indexPath.row];
  return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  if (self.selectedCellBlock) {
    self.selectedCellBlock(indexPath, self.dataArray);
  }
}
#pragma mark -- getter
- (UITableView *)tableView{
  if (!_tableView) {
    _tableView = [UITableView new];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    UITapGestureRecognizer *tableViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tableViewTouchInSide)];
    tableViewGesture.numberOfTapsRequired = 1;//几个手指点击
    tableViewGesture.cancelsTouchesInView = NO;//是否取消点击处的其他action
    [_tableView addGestureRecognizer:tableViewGesture];
  }
  return _tableView;
}
// ------tableView 上添加的自定义手势
- (void)tableViewTouchInSide{
  // ------结束编辑，隐藏键盘
  [self.superview endEditing:YES];
}

- (void)setDataArray:(NSArray *)dataArray{
  _dataArray = dataArray;
  [self.tableView reloadData];
  [self gj_hiddenNodataView];
  if (dataArray.count > 0) {
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
  }else{
    [self.tableView gj_showNodataViewWithImage:[UIImage imageNamed:@"mapModule_noneData"] text:@"暂无数据" retryButtonTitle:@"" retryButtonBlock:^{
      
    }];
  }
}
@end
